import { getData } from '@vimi/utils-api'
import { SERVICE_NAME, IOT_SERVICE_NAME } from '@/credit/plugins/constant'

export const enumCustomerStatus = {
  1: '白名单',
  2: '黑名单',
  3: '灰名单',
}
export const enumStatusStyle = {
  1: 'success',
  2: 'danger',
  3: 'warning',
}
export const enumRiskStatus = {
  1: '正常',
  2: '关注',
  3: '次级',
  4: '可疑',
  5: '损失',
}

export const dataAdapter = item => {
  item.is_gamble = +item.is_gamble || 0 // 是否赌博
  item.is_drunk = +item.is_drunk || 0 // 是否酗酒
  item.is_court_exe = +item.is_court_exe || 0 // 是否法院被执行
  item.is_dis_list = +item.is_dis_list || 0 // 是否在失信人名单
  item.is_litigate = +item.is_litigate || 0 // 是否涉诉
  item.is_comps = +item.is_comps || 0 // 是否代偿
  item.risk_type = +item.risk_type || 1 // 五级分类 1正常 2关注 3次级 4可疑 5损失

  item.customerStatus =
    [item.is_gamble, item.is_drunk, item.is_court_exe, item.is_dis_list, item.is_comps].includes(
      1
    ) || item.risk_type === 5
      ? 2
      : [3, 4].includes(item.risk_type) || item.is_litigate === 1
      ? 3
      : 1

  item.customerStatusLabel = enumCustomerStatus[item.customerStatus]

  return item
}

export const getParamsFromStatus = status => {
  switch (status) {
    case 1:
      return {
        'is_gamble.eq': 0,
        'is_drunk.eq': 0,
        'is_court_exe.eq': 0,
        'is_dis_list.eq': 0,
        'is_comps.eq': 0,
        'is_litigate.eq': 0,
        'risk_type.in': '0,1,2',
      }
    case 2:
      return {
        'orWhere.is_gamble.eq': 1,
        'orWhere.is_drunk.eq': 1,
        'orWhere.is_court_exe.eq': 1,
        'orWhere.is_dis_list.eq': 1,
        'orWhere.is_comps.eq': 1,
        'orWhere.risk_type.eq': 5,
      }
    case 3:
      return {
        'is_gamble.eq': 0,
        'is_drunk.eq': 0,
        'is_court_exe.eq': 0,
        'is_dis_list.eq': 0,
        'is_comps.eq': 0,
        'orWhere.is_litigate.eq': 1,
        'orWhere.risk_type.in': '3,4',
      }
    default:
      return {}
  }
}

const checkEleTable = farmId =>
  getData(SERVICE_NAME, 'datum_customer_ele_table', {
    index: 1,
    size: 1,
    'customer_id.eq': farmId,
  }).then(res => (res?.data?.totalCount ?? 0) > 0)
const checkDevice = farmId =>
  getData(IOT_SERVICE_NAME, 'place_info', {
    customer_id: farmId,
    subKey: 'device_info.place_info_id',
  }).then(res => !!(res?.data?.[0] ?? {}).device_info)

export const checkDevices = farmId =>
  Promise.all([checkEleTable(farmId), checkDevice(farmId)]).then(
    ([hasEle, hasDevice]) => hasEle && hasDevice
  )

export const personalOrgClass = ['org_farmer', 'org_farm', 'org_large_breed']
